package com.fyzs.mp3recorder

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.fyzs.mp3recorder.wav.WavDecoder
import java.io.File

class PickupFragment : Fragment(), View.OnClickListener {



    lateinit var recyclerView:RecyclerView

    lateinit var swipeRecyclerView: SwipeRefreshLayout


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_pickup, container, false)
    }
    val fileListAdapter = FileListAdapter(object : DiffUtil.ItemCallback<File>() {
        override fun areItemsTheSame(oldItem: File, newItem: File): Boolean {
            return false
        }

        override fun areContentsTheSame(oldItem: File, newItem: File): Boolean {
            return false
        }
    })
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        swipeRecyclerView = view.findViewById(R.id.swipe_refresh_layout)
        swipeRecyclerView.setOnRefreshListener {
            swipeRecyclerView.isRefreshing = false
            val readFileList = readFileList()
            fileListAdapter.submitList(readFileList)
        }

        recyclerView = view.findViewById(R.id.recycler_view)

        recyclerView.adapter =fileListAdapter
        val readFileList = readFileList()
        fileListAdapter.submitList(readFileList)
    }

    fun readFileList():List<File>{
        val cacheDir = requireActivity().cacheDir

        val filter = cacheDir.listFiles().toList()
        return filter?: emptyList()
    }

    inner class FileListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val nameTv:TextView = itemView.findViewById(R.id.file_name)
    }

    inner class FileListAdapter(diffCallback: DiffUtil.ItemCallback<File>) :
        ListAdapter<File, FileListViewHolder>(diffCallback) {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileListViewHolder {
            val inflate = LayoutInflater.from(parent.context)
                .inflate(R.layout.audio_file_layout, parent, false)
            return FileListViewHolder(inflate)
        }

        override fun onBindViewHolder(holder: FileListViewHolder, position: Int) {
            val file = currentList[position]

            val wavDecoder = WavDecoder(file)
            //
            holder.nameTv.text = currentList[position].name
            //

            holder.itemView.setOnClickListener{
                val intent = Intent(requireActivity(), AudioPlayActivity::class.java).apply {
                    putExtra("filePath", currentList[position].path)
                }
                this@PickupFragment.startActivity(intent)
            }
        }
    }

    override fun onClick(v: View?) {

    }
}